package com.sailfish.springbootdemo.dao.db4;

import com.sailfish.springbootdemo.common.*;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface OperateDao {

    /**
     * 查询所有
     *
     * @return
     */
    @Results(id = "companyResultMap", value = {
            @Result(column = "operate_id", property = "operateId", jdbcType = JdbcType.INTEGER),
            @Result(column = "operate_big_type", property = "operateBigType", jdbcType = JdbcType.INTEGER),
            @Result(column = "operator_id", property = "operatorId", jdbcType = JdbcType.INTEGER),
            @Result(column = "operate_small_type", property = "operateSmallType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "operate_data_type", property = "operateDataType", jdbcType = JdbcType.INTEGER),
            @Result(column = "operate_data_type_notes", property = "operateDataTypeNotes", jdbcType = JdbcType.INTEGER),
            @Result(column = "operate_time", property = "operateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "before_data", property = "beforeData", jdbcType = JdbcType.VARCHAR),
            @Result(column = "after_data", property = "afterData", jdbcType = JdbcType.VARCHAR),
            @Result(column = "nick_name_cn", property = "nickNameCn", jdbcType = JdbcType.VARCHAR),
    })
    @Select("<script>" +
            "select t1.* from " + DB4TableName.OPERATE + " t1 left join " + DB4TableName.USER + " t2 on t1.operator_id = t2.user_id where 1 = 1 " +
            "<if test='operateBigType != null and operateBigType != \"\" '>" +
            " and t1.operate_big_type = #{operateBigType}" +
            "</if>" +
            "<if test='nickNameCn != null and nickNameCn != \"\" '>" +
            " and (t2.nick_name_cn like CONCAT('%',#{nickNameCn},'%') or t2.display_name like CONCAT('%',#{nickNameCn},'%') or t2.domain_account like CONCAT('%',#{nickNameCn},'%'))" +
            "</if>" +
            "<if test='operateDataType != null and operateDataType != \"\" '>" +
            " and t1.operate_data_type = #{operateDataType}" +
            "</if>" +
            "<if test='operateDataTypeNotes != null and operateDataTypeNotes != \"\" '>" +
            " and t1.operate_data_type_notes = #{operateDataTypeNotes}" +
            "</if>" +
            " order by t1.operate_time desc" +
            "<if test='begin != null and pageSize != null and pageSize != \"\" '>" +
            " limit #{begin},#{pageSize}" +
            "</if>" +
            "</script>")
    List<Operate> getAllOperate(@Param("begin") Integer begin, @Param("pageSize") Integer pageSize, @Param("operateBigType") String operateBigType, @Param("nickNameCn") String nickNameCn, @Param("operateDataType") String operateDataType, @Param("operateDataTypeNotes") String operateDataTypeNotes);

    /**
     * 新增
     *
     * @param operate
     * @return
     */
    @Insert("insert into " + DB4TableName.OPERATE + "(operate_big_type,operator_id,operate_small_type,operate_data_type,operate_data_type_notes,operate_time,before_data,after_data,nick_name_cn) values " +
            "(#{operate.operateBigType},#{operate.operatorId},#{operate.operateSmallType},#{operate.operateDataType},#{operate.operateDataTypeNotes},#{operate.operateTime},#{operate.beforeData},#{operate.afterData},#{operate.nickNameCn})")
    Integer addOperate(@Param("operate") Operate operate);
}
